<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="厂商名称" prop="manufacturerName">
      <el-input v-model="dataForm.manufacturerName" placeholder="厂商名称"></el-input>
    </el-form-item>
    <el-form-item label="厂商编码" prop="manufacturerCode">
      <el-input v-model="dataForm.manufacturerCode" placeholder="厂商编码"></el-input>
    </el-form-item>
    <el-form-item label="厂商类型" prop="type">
      <el-select v-model="dataForm.type" placeholder="请选厂商类型">
        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
      </el-select>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/pbms/arcmanufacturer'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          manufacturerId: 0,
          manufacturerName: '',
          manufacturerCode: '',
          type: ''
        },
        dataRule: {
          manufacturerName: [
            { required: true, message: '厂商名称不能为空', trigger: 'blur' },
            { min: 2, max: 60, message: "长度在 2 到 60 个字符", trigger: "blur" }
          ],
          manufacturerCode: [
            { required: true, message: '厂商编码不能为空', trigger: 'blur' },
            { min: 2, max: 30, message: "长度在 2 到 30 个字符", trigger: "blur" }
          ],
          type: [
            { required: true, message: '厂商类型不能为空', trigger: 'blur' }
          ]
        },
        options: [{
          value: 1,
          label: '设备厂商'
        }, {
          value: 2,
          label: '装置厂商'
        }],
      }
    },
    methods: {
      init (id) {
        this.dataForm.manufacturerId = id || 0;
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.manufacturerId) {
            getObj(this.dataForm.manufacturerId).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.manufacturerId) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      }
    }
  }
</script>
